<template>
  <ea-dialog
      v-bind="$attrs"
      v-on="$listeners"
      width="400px"
      ref="elDialog"
      title="设置题库"
      :type="type"
      @submit="submit"
      @reset="$refs.form.resetFields()"
      @closed="end"
  >
    <el-form ref="form" :rules="rules" :model="singleData" label-width="100px">
      <template v-if="type === 'add'">
        <el-form-item label="选择学校" prop="schoolCode">
          <el-select
              v-model="singleData.schoolCode"
              @change="getDept"
              @clear="clearSchool"
              filterable
              clearable
              placeholder="请选择学校"
          >
            <el-option
                v-for="item in schoolList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择院系" prop="deptCode">
          <el-select
              v-model="singleData.deptCode"
              filterable
              placeholder="请选择院系"
              @clear="clearDept"
              @change="getClass"
              clearable
          >
            <el-option
                v-for="item in deptList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择班级" prop="classCode">
          <el-select
              v-model="singleData.classCode"
              filterable
              placeholder="请选择班级"
              clearable
          >
            <el-option
                v-for="item in classList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </template>
      <el-form-item label="选择题库" prop="bankids">
        <el-select v-model="singleData.bankids" multiple placeholder="选择题库">
          <el-option
              v-for="item in queBanks"
              :key="item.value"
              :label="item.name"
              :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </ea-dialog>
</template>

<script>
import editAndAdd from "@/mixins/business/editAndAdd";
import schoolDeptClassSelection from "@/mixins/business/schoolDeptClassSelection"

export default {
  name: "EaStuQueBank",
  mixins: [editAndAdd, schoolDeptClassSelection],
  data() {
    return {
      singleData: {
        bankids: [],
        classCode: "",
        schoolCode: "",
        deptCode: "",
        type: 0,
        userid: 0
      },
      rules: {
        classCode: {required: false, message: "请选择班级！"},
        schoolCode: {required: true, message: "请选择学校！"},
        deptCode: {required: false, message: "请选择院系！"},
        bankids: {required: true, message: "请选择题库！"}
      },
      targetObj: 'singleData',//school dept class 操作对象
      queBanks: []
    };
  },
  watch: {
    "singleData.classCode"(newVal) {
      this.getQueBank(newVal)
    },

    "singleData.schoolCode"(newValschool) {
      this.getQueBank(newValschool)
    },

    "singleData.deptCode"(newValdeptCode) {
      this.getQueBank(newValdeptCode)
    }
  },
  methods: {
    getQueBank(classCode,newValschool,newValdeptCode) {
			
      if (classCode ||newValschool||newValdeptCode) {
        this.axios
            .get("/sys/stuQueBank/getUserBank", {params: {classCode,newValschool,newValdeptCode}})
            .then(res => {
							this.queBanks = res});
      }
			 // else this.emptyQueBank();清空
    },
    emptyQueBank() {
      this.queBanks.splice(0);
      this.singleData.bankids.splice(0);
    },
    handle() {
      if (this.type === "add") this.singleData.userid = null;
      else this.singleData.classCode = null;
      return this.axios.post("/sys/stuQueBank/saveStuBank", {
        ...this.singleData,
        type: this.type === "add" ? 1 : 2
      });
    }
  }
};
</script>

<style scoped></style>
